package com.zd.lib.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.zd.lib.utils.LogUtils;


/**
 * @author zoudong
 *         Glide  圆形转换
 *         glideRequest.load("https://www.baidu.com/img/bdlogo.png").transform(new GlideCircleTransform(context)).into(imageView);
 */
public class GlideUtils {

    public static class FillSpace extends BitmapTransformation {


        public FillSpace(Context context) {
            super(context);
        }

        public FillSpace(BitmapPool bitmapPool) {
            super(bitmapPool);
        }

        @Override
        public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            if (toTransform.getWidth() == outWidth && toTransform.getHeight() == outHeight) {
                return toTransform;
            }
            Matrix matrix = new Matrix();
            final float widthPercentage = (float)outWidth / (float) toTransform.getWidth();
            final float heightPercentage = (float)outHeight / (float) toTransform.getHeight();
            float scale = Math.max(widthPercentage, heightPercentage);
            LogUtils.d("zoudong", "transform: widthPercentage="+widthPercentage+"--heightPercentage"+heightPercentage+"---scale"+scale);
//            matrix.setScale(scale, scale);
            matrix.setScale(widthPercentage, heightPercentage);
            Bitmap result = pool.get(outWidth, outHeight, toTransform.getConfig() != null
                    ? toTransform.getConfig() : Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(outWidth, outHeight, toTransform.getConfig() != null
                        ? toTransform.getConfig() : Bitmap.Config.ARGB_8888);
            }
            // We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
            TransformationUtils.setAlpha(toTransform, result);
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint(TransformationUtils.PAINT_FLAGS);
            canvas.drawBitmap(toTransform, matrix, paint);
            return result;
        }

        @Override
        public String getId() {
            return "FillSpace.com.bumptech.glide.load.resource.bitmap";
        }
    }



}
